﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NLua;
using System.IO;

namespace WindowsFormsApplication1
{
    class RunLua
    {
        Lua lua;

        public RunLua()
        {
            lua = new Lua();
        }

        public void Start()
        {
            TestVar();
            TestFunc();
            TestObjectA();
            TestObjectB();
            TestFile();
        }

        void TestVar()
        {
            //测试变量赋值
            lua.DoString("var1 = 10");

            double n1 = (double)lua["var1"];
            Console.WriteLine(n1);

            double val = 10;
            lua["val"] = val;
            lua.DoString("x = val * 2");
            double result = (double)lua["x"];
            Console.WriteLine(result);


        }

        void TestFunc()
        {
            //测试函数调用
            lua.DoString(@"
                            function ScriptFunc (val1, val2)
                                if val1 > val2 then
                                    return val1 + 1
                                else
                                    return val2 - 1
                                end
                            end
                        ");

            var scriptFunc = lua["ScriptFunc"] as LuaFunction;
            double result = (double)scriptFunc.Call(3, 5).First();

            Console.WriteLine(result);
        }


        void TestObjectA()
        {
            //测试对象示例
            TestClass tc = new WindowsFormsApplication1.TestClass();
            lua["tc"] = tc;

            lua.DoString(@" 
                            tc.PP = 'Hello'
                            res = tc:Add(10, 20)
                            helloText = tc:Hello('TT')
                        ");

            double result = (double)lua["res"];
            Console.WriteLine(string.Format("{0}, {1}", result, tc.PP));

            string helloText = lua["helloText"] as string;
            Console.WriteLine(helloText);
            
        }

        void TestObjectB()
        {
            //测试对象示例
            lua.LoadCLRPackage();
            lua["msgText"] = "Message Text";

            lua.DoString(@"import ('WindowsFormsApplication1')");
            lua.DoString(@"                            
                            tc = TestClass()
                            --tc:ShowMessage(msgText)
                            res = tc:Add(10, 20)
                            textLen = TestClass.GetLength(msgText)
                            tc.PP = 'ABC'
                            res = res + TestClass.GetLength(tc.PP)
                        ");

            double result = (double)lua["res"];
            double len = (double)lua["textLen"];
            Console.WriteLine(string.Format("{0}, {1}", result, len));

        }

        void TestFile()
        {
            //测试运行脚本文件
            string filePath = "sample1.lua";

            bool boState = true;
            try
            {
                ConsolePrint cp = new ConsolePrint();
                lua.RegisterFunction("Print", cp, cp.GetType().GetMethod("Print"));


                lua.DoFile(filePath);
                LuaFunction func = lua["ConsoleWriteline"] as LuaFunction;
                if (func != null)
                {
                    object[] resValues =func.Call("ABC123\r\nDDD");

                    bool boRes = (bool)resValues[0];
                }


                NComLib nclib = new NComLib();
                lua.RegisterFunction("Delay", nclib, nclib.GetType().GetMethod("Delay"));

                LuaFunction delayTest = lua["DelayTest"] as LuaFunction;
                if(delayTest != null)
                {
                    delayTest.Call(10);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                boState = false;
            }

            if (boState)
            {
                double res = (double)lua["res"];
                Console.WriteLine(res);
            }
        }
    }

    public class TestClass
    {
        public string PP { get; set; }

        public double Add(double x, double y)
        {
            return x + y;
        }

        public void ShowMessage(string text)
        {
            System.Windows.Forms.MessageBox.Show(text);
        }

        public string Hello(string text)
        {
            return "Hello: " + text;
        }

        public static double GetLength(string text)
        {
            return text.Length;
        }
    }

    class ConsolePrint
    {
        public void Print(string text)
        {
            Console.WriteLine(text);
        }
    }

    class NComLib
    {
        public void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)
            {
                System.Windows.Forms.Application.DoEvents();
            }
        }
    }
}
